<?php
namespace app\admin\controller;
use think\Db;

class Operation extends Common
{
	public function index(){

		$data = model("Operation")->getDataAll(1);
        //全部信息
        $this->assign('data',$data);
 
        $major = Db::name('major')->where('status',1)->select();
	    $this->assign('major',$major);

	    $subject = Db::name('subject')->where('status',1)->select();
		$this->assign('subject',$subject);

		return $this->fetch();
	}

	 public function major(){
    	$major_id = input('post.major_id');
    	$data = Db::name('subject')->where('major_id',$major_id)->select();
    	return json($data);
    } 


    public function upload(){
    	$id = input('get.id');

    	$file = request()->file('operation');
    	$ex = explode('.',$_FILES['operation']['name']);
    	// echo end($ex);die;
    	// implode(glue, pieces)
    	if ($file) {
    		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'operation'.DS.$id);
    		$type = $info->getExtension();
    		if($info){
    			Db::name('operation')->where('id',$id)->update(['pathinfo'=>'public/uploads'.DS.'operation'.DS.$id.DS.$info->getSaveName()]);
    			$this->success('上传成功');
    		}else{
    			$this->error($file->getError());
    		}

    		
    	}else{
    		$this->error('错误类型');
    	}
    }

    public function download($id=""){
    	$pathinfo = Db::name('operation')->where('id',$id)->field('pathinfo')->find();
    	$pathinfo = implode(',',$pathinfo);
    	// echo $pathinfo;die;
    	// header("Content-type:application/xlsx");

    	$load = ROOT_PATH.$pathinfo;
    	// $load = str_replace('\\', '/', $load);
    	// header("Content-type:application/octet-stream");
    	header("Content-Disposition:attachment;filename=".$load);
    	readfile("$load");
    }


     //软删除
    public function del(){
    	//获取的是一个数组
    	$ids = input('post.ids/a');
    	//将数组转为字符串 以逗号分隔
    	$ids = implode(',',$ids);
    	//软删除 更新状态
    	$result = Db::name('operation')->where("id in ($ids)")->setField('status',0);
    	if($result){
    		$this->success("删除成功");
    	}else{
    		$this->error("删除失败");
    	}
    }


    public function add(){
        if (request()->isPost()) {
             $data = input('post.data/a');
             // dump($data);die;
             if(!$data){
                return json(array('code'=>0,'msg'=>"参数错误"));
             }
             $data['add_time'] = date('Y-m-d H:i:s');
             $result = model('single')->addSingle($data);

             if($result){
                    return json(['code'=>1,'msg'=>'添加成功']);
             }else{
                    return json(['code'=>0,'msg'=>'添加失败']);
             }


        }else{
            $major = Db::name('major')->where('status',1)->select();
            $this->assign('major',$major);

            $subject = Db::name('subject')->where('status',1)->select();
            $this->assign('subject',$subject);

            return $this->fetch();
        }
    
    }

}